package com.relax.no_xml.postProcessor;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.context.annotation.Configuration;


/**
 * 在bean构造方法之前调用这个类,比如可以判断是否使用aop
 *   调用时机 -->  refresh()  -> finishBeanFactoryInitialization(beanFactory);
 *    -->getBean(beanName)  -->  doGetBean(name) --> createBean(beanName, mbd, args);
 *    -->	Object bean = resolveBeforeInstantiation(beanName, mbdToUse)
 *    --> applyBeanPostProcessorsBeforeInstantiation(targetType, beanName);
 *\
 *

 */
@Configuration
public class PqsBeanPostProcessor implements BeanPostProcessor {


	/**
	 * 调用时机
	 * 1.class反射
	 * 2.populate 赋值之后
	 * 3.todo 该方法调用
	 * 4. 执行init方法
	 * 5.执行postProcessAfterInitialization
	 */
	@Override
	public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
		System.out.println("PqsBeanPostProcessor==>bean's postProcessor======BeforeInitialization,beanName=>"+beanName+",bean ="+bean);
		if(beanName.equals("person")){
			System.out.println(" remove  person");
			return null;
		}
		return bean;
	}


	/**
	 * 调用时机
	 * 1.class反射
	 * 2.populate 赋值之后
	 * 3.postProcessBeforeInitialization
	 * 4. 执行init方法
	 * 5. todo 执行该方法
	 */
	@Override
	public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
		System.out.println("PqsBeanPostProcessor==>bean's postProcessor---AfterInitialization,beanName=>"+beanName+",bean ="+bean);
		return bean;
	}


	public PqsBeanPostProcessor(){
		System.out.println("PqsBeanPostProcessor 构造函数执行....");
	}
}
